package com.qf.dao.impl;

import com.qf.dao.GoodDao;
import com.qf.entity.Good;
import com.qf.entity.Page;
import com.qf.util.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2020/11/17 20:53
 * @Version 1.0
 */
public class GoodDaoImpl implements GoodDao {
    private QueryRunner qr=new QueryRunner();
    //查询该商品类型标签下商品的数量
    @Override
    public int selectCountByTypeId(Integer typeId) throws SQLException {
        String sql="select count(*) from tb_good where typeId=?";
        long query=0;
        try {
            query= (long) qr.query(DbUtils.getConnection(),sql,new ScalarHandler(),typeId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return (int)query;
    }

    @Override
    public List<Good> selectLimitGoodsByTypeId(Integer typeId, Page page) {
        String sql="select * from tb_good where typeId=? limit ?,?";
        List<Good> query=null;
        try {
            query= qr.query(DbUtils.getConnection(),sql,new BeanListHandler<>(Good.class),typeId,page.getStartRows(),page.getPageSize());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return query;
    }

    @Override
    public Good selectGoodById(int id) {
        String sql="select * from tb_good where id=?";
        Good query=null;
        try {
            query=qr.query(DbUtils.getConnection(),sql,new BeanHandler<>(Good.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return query;
    }
}
